/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Secure.java
 *
 * Created on Oct 8, 2010, 9:21:51 AM
 */

package cat200;


import java.sql.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.SpinnerNumberModel;
/**
 *
 * @author alex
 */
public class Secure extends javax.swing.JFrame {

    private String url = "jdbc:derby://localhost:1527/sample";
    private String userID = "app";
    private String userPW = "app";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;

    /** Creates new form Secure */
    public Secure() {
    	super("Authentication");
        initComponents();
        setLocationRelativeTo(null);
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        
    }

    private void connectDatabase()
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            con=DriverManager.getConnection(url, userID, userPW);
        }


        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(CAKE1.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (SQLException ex)
        {
            Logger.getLogger(CAKE1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private ResultSet getResultSet(String doubt)
    {
        connectDatabase();
        ResultSet outcome = null;
        try
        {
            stmt = con.createStatement();
            outcome = stmt.executeQuery(doubt);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(CAKE1.class.getName()).log(Level.SEVERE, null, ex);
        }
        return outcome;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField1 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 300));
        setResizable(false);

        jPanel1.setLayout(null);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 3, 24));
        jLabel4.setForeground(new java.awt.Color(223, 46, 192));
        jLabel4.setText("JANICE BAKERY SHOP");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(60, 30, 310, 28);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(10, 70, 380, 20);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        jPanel1.add(jTextField1);
        jTextField1.setBounds(180, 100, 150, 25);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel6.setText(" EMPLOYEE ID : ");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(50, 96, 130, 30);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel5.setText("PASSWORD : ");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(70, 150, 130, 16);

        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });
        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordField1KeyPressed(evt);
            }
        });
        jPanel1.add(jPasswordField1);
        jPasswordField1.setBounds(180, 150, 150, 25);

        jButton3.setText("CANCEL");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(190, 260, 90, 27);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 15));
        jLabel2.setForeground(new java.awt.Color(248, 15, 36));
        jLabel2.setText("INVALID USERNAME OR PASSWORD!");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(40, 190, 327, 21);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 15));
        jLabel3.setForeground(new java.awt.Color(248, 15, 36));
        jLabel3.setText("PLEASE TRY AGAIN!");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(110, 220, 179, 21);

        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);
        jButton2.setBounds(290, 260, 90, 27);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jTextField1.setText("");
        jPasswordField1.setText("");
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
}                                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
    }                                               

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String userid,password;
        userid=jTextField1.getText();
        password=jPasswordField1.getText();
        System.out.println(userid);
        System.out.println(password);

        if((userid == null ? "" == null : userid.equals("")) && (password == null ? "" == null : password.equals(""))){
            jTextField1.setText("");
            jPasswordField1.setText("");
        } else {

            //Java code for matching the input with database//
            try
        {
            int col1=0;
            String col2="";
            connectDatabase();

            rset= getResultSet("SELECT EMP_ID,EMP_POSITION FROM APP.EMPLOYEE WHERE EMP_ID = "+userid+" AND EMP_PASSWORD = '"+password+"'");
            System.out.println(rset);

            if(rset.next())
            {
                col1 = rset.getInt(1);
                col2 = rset.getString(2);

                if(col1 ==0 && col2 == null){
                jTextField1.setText("");
                jPasswordField1.setText("");
                jLabel2.setVisible(true);
                jLabel3.setVisible(true);
                }

                System.out.println(col1);
                System.out.println(col2);
                
                this.setVisible(false);
                new Main(col1).setVisible(true);
            }
            else
            {
                jTextField1.setText("");
                jPasswordField1.setText("");
                jLabel2.setVisible(true);
                jLabel3.setVisible(true);
            }
            con.close();
        }
        catch(Exception e)
        {
            jTextField1.setText("");
            jPasswordField1.setText("");
            jLabel2.setVisible(true);
            jLabel3.setVisible(true);
        }
        }
}                                        

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
            jLabel2.setVisible(false);
            jLabel3.setVisible(false);
    }                                      

    private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {                                           
        // TODO add your handling code here:
            jLabel2.setVisible(false);
            jLabel3.setVisible(false);
    }                                          

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Secure().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   

}
